﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	/*
	 * coded by moka 2009 / 07/ 25 
	 * name : moka's earth 
	 * blog : http://designplusplus.blogspot.com
	 */
	
	public class Main extends Sprite 
	{		
		private var bg:Bitmap;
		private var scene:Scene3D;
		private var viewport:Viewport3D;
		private var camera:Camera3D;
		private var render:BasicRenderEngine;
		private var plane:Plane;
		private var planeDummy:Plane;
		private var sphereDummy:Sphere;
		private var flag:Boolean;

		[Embed(source = 'earth.jpg')]
		private var EmbeddedImage:Class;
		private var image:Bitmap;
		
		
		public function Main():void 
		{		
			image = new EmbeddedImage(); 
			
			// stage setting
			stage.scaleMode = "noScale";
			
			// background
			bg = addChild(new Bitmap(new BitmapData(640, 480, false, 0x000000)))as Bitmap;
			
			// pv3d essential setting
			scene = new Scene3D();
			camera = new Camera3D();
			viewport = new Viewport3D();
			render = new BasicRenderEngine();	
			viewport.filters = [new GlowFilter(0x0033FF,1,10,10,3,3)]
		
			var segW:uint = 16;
			var segH:uint = 12;
			
			// dummys
			planeDummy = new Plane(null, 1500, 750, segW, segH);
			sphereDummy = new Sphere(null, 300, segW, segH);
			
			// earth
			var wm:BitmapMaterial = new BitmapMaterial(image.bitmapData)
			wm.doubleSided = true;
			plane = scene.addChild(new Plane(wm, 640, 480, segW, segH)) as Plane;			
			
			// click event
			flag = false;

			// Moka's work 2009
			var moka:TextField = addChild(new TextField()) as TextField;
			moka.htmlText = "<font color='#FFFFFF'>Moka's work</font>";
		
			//event registration
			stage.addEventListener(Event.ENTER_FRAME, onProcess);
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}		
		
		private function onClick(e:MouseEvent):void {
			flag = !flag
		}

		private function onProcess(e:Event):void {
			bg.bitmapData.fillRect(bg.bitmapData.rect, 0x000000);
			plane.rotationX = mouseX;
			plane.rotationY = mouseY;
			var ease:uint = 5;
			
			if (flag) {
				// transform to plane by using
				for (var n:uint = 0; n < plane.geometry.vertices.length;n++ ){
					plane.geometry.vertices[n].x += (planeDummy.geometry.vertices[n].x-plane.geometry.vertices[n].x)/ease;
					plane.geometry.vertices[n].y += (planeDummy.geometry.vertices[n].y-plane.geometry.vertices[n].y)/ease;
					plane.geometry.vertices[n].z += (planeDummy.geometry.vertices[n].z-plane.geometry.vertices[n].z)/ease
				}
			}else {
				// transform to sphere
				var segW:uint = 16;
				var segH:uint = 12;
				var pLength:uint = plane.geometry.vertices.length;
				var sLength:uint = sphereDummy.geometry.vertices.length;	
				var lArray:Array = new Array();
				for (var m:uint = 0; m < (segH+1) - 2;m++ )lArray.push(1 + m * segW);				
				for (var i:uint = 0; i < pLength; i++ ) {
						for (var k:uint = 1; k <= (segH+1) - 2;k++ )
						if (i % (segH+1) == k) {						
							if (i == segW*(segH+1)+k) {
								plane.geometry.vertices[i].x += (sphereDummy.geometry.vertices[lArray[k-1]-segW].x-plane.geometry.vertices[i].x)/(ease*k*Math.random()+1);
								plane.geometry.vertices[i].y += (sphereDummy.geometry.vertices[lArray[k-1]-segW].y-plane.geometry.vertices[i].y)/(ease*k*Math.random()+1);
								plane.geometry.vertices[i].z += (sphereDummy.geometry.vertices[lArray[k-1]-segW].z-plane.geometry.vertices[i].z)/(ease*k*Math.random()+1);
							}else {
								plane.geometry.vertices[i].x += (sphereDummy.geometry.vertices[lArray[k-1]].x-plane.geometry.vertices[i].x)/(ease*k*Math.random()+1);
								plane.geometry.vertices[i].y += (sphereDummy.geometry.vertices[lArray[k-1]].y-plane.geometry.vertices[i].y)/(ease*k*Math.random()+1);
								plane.geometry.vertices[i].z += (sphereDummy.geometry.vertices[lArray[k-1]].z-plane.geometry.vertices[i].z)/(ease*k*Math.random()+1);							
							}
							lArray[k-1]++;
						}else if (i % (segH+1) == 0) {
							plane.geometry.vertices[i].x += (sphereDummy.geometry.vertices[0].x-plane.geometry.vertices[i].x)/ease;
							plane.geometry.vertices[i].y += (sphereDummy.geometry.vertices[0].y-plane.geometry.vertices[i].y)/ease;
							plane.geometry.vertices[i].z += (sphereDummy.geometry.vertices[0].z-plane.geometry.vertices[i].z)/ease;
						}else if (i % (segH+1) == segH){
							plane.geometry.vertices[i].x += (sphereDummy.geometry.vertices[sLength-1].x-plane.geometry.vertices[i].x)/ease;
							plane.geometry.vertices[i].y += (sphereDummy.geometry.vertices[sLength-1].y-plane.geometry.vertices[i].y)/ease;
							plane.geometry.vertices[i].z += (sphereDummy.geometry.vertices[sLength-1].z-plane.geometry.vertices[i].z)/ease;
						}				
				}
			}
			
			render.renderScene(scene, camera, viewport);
			bg.bitmapData.draw(viewport); // draw current view of camera to bg
		}
	}		
}